package com.neu.tly.utils;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import com.neu.tly.model.User;

public class JdbcUtils {
	
		private static String driverName="com.mysql.cj.jdbc.Driver";
		private static String url="jdbc:mysql://localhost:3308/test?useUnicode=true&characterEncoding=utf-8&useSSL=false&serverTimezone=GMT%2B8";
		private static String user="root";
		private static String password="tly123123";
		
		private static Connection conn=null;
		private static Statement stmt=null;
		private static PreparedStatement pst=null;
		private static ResultSet rs=null;
	
	
	public static Connection getConnection() throws SQLException {
		return DriverManager.getConnection(url, user, password);
	}
	
	public static int insertAndUpdate(String sql) throws SQLException {
		conn=getConnection();
		stmt=conn.createStatement();
		return stmt.executeUpdate(sql);
	}
	
	public static List<User> query(String sql) throws SQLException {
		conn=getConnection();
		stmt=conn.createStatement();
		rs=stmt.executeQuery(sql);
		/*
		 * List<User> ul=new ArrayList<>(); while(rs.next()) { User u=new User();
		 * u.setId(rs.getInt(1)); u.setUserName(rs.getString(2));
		 * u.setPassword(rs.getString(3)); ul.add(u); }
		 */
	    return ResultSetToListUtil.resultToList(rs, User.class);
	}

}
